package com.bbshop.dao.product.impl;

import java.util.LinkedHashMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bbshop.bean.common.AbstractSearchCondition;
import com.bbshop.bean.common.SearchCondition;
import com.bbshop.bean.product.ProductInfo;
import com.bbshop.bean.product.ProductSearchCondition;
import com.bbshop.dao.base.DAOSupport;
import com.bbshop.dao.product.ProductDAO;
@Component
@Transactional
public class ProductDAOImpl extends DAOSupport<ProductInfo> implements ProductDAO {
	@Override
	public CriteriaQuery<ProductInfo> dealSearchCondition(CriteriaBuilder cb,
			SearchCondition<ProductInfo> sc) {
		
		CriteriaQuery<ProductInfo> cq = cb.createQuery(ProductInfo.class);
		Root<ProductInfo> root = cq.from(ProductInfo.class);
		//System.out.println(sc);
		ProductSearchCondition usc = (ProductSearchCondition) sc;
		LinkedHashMap<String, String> orderby = usc.getOrderBy();
		for (String key : orderby.keySet()) {
			String orderreg = orderby.get(key);
			if (AbstractSearchCondition.ORDER_ASC.equals(orderreg)) {
				cq.orderBy(cb.asc(root.get(key)));
			}
			if (AbstractSearchCondition.ORDER_DESC.equals(orderreg)) {
				cq.orderBy(cb.desc(root.get(key)));
			}
		}
		return cq;
	}
}
